<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file license.txt or that can be found at 
 * http://code.google.com/p/billboardce/wiki/License.
 *
 * @package    Slide
 * @category   Model
 * @license    http://code.google.com/p/billboardce/wiki/License New BSD License
 * @version    SVN: $Id: $
 */
class Slide extends Ot_Db_Table
{
    /**
     * Name of the table in the database
     *
     * @var string
     */
    protected $_name = 'tbl_slide';
  
    /**
     * Primary key of the table
     *
     * @var string
     */
    protected $_primary = 'slideId';
    
    /**
     * Generates the form for adding and editing a slide
     * 
     * @param array $values The values the form should be populated with
     * @return Zend_Form
     */
    public function form ($values = array())
    {
        $form = new Zend_Form();
        $form->setAttrib('id', 'slideForm')
             ->setDecorators(array(
                     'FormElements',
                     array('HtmlTag', array('tag' => 'div', 'class' => 'zend_form')),
                     'Form',
             ));
                
        // Create and configure element for the title of the slide:
        $title = $form->createElement('text', 'title', array('label' => 'Title:'));
        $title->setRequired(true)
              ->addFilter('StringTrim')
              ->addFilter('StripTags')
              ->setAttrib('maxlength', 255)
              ->setValue((isset($values['title'])) ? $values['title'] : '')
              ->addValidator('NotEmpty', true, array('messages' => array('isEmpty' => 'Must not be blank!')));

        // Create and configure the element for the url corresponding to the slide
        $url = $form->createElement('text', 'url', array('label' => 'URL:'));
        $url->setRequired(true)
            ->addFilter('StringTrim')
            ->addFilter('StripTags')
            ->setAttrib('maxlength', 255)
            ->setValue((isset($values['url'])) ? $values['url'] : '')
            ->addValidator('NotEmpty', true, array('messages' => array('isEmpty' => 'Must not be blank!')))
            ->addValidator(new Ot_Validate_Uri());
            
        $submit = $form->createElement('submit', 'submitButton', array('label' => 'Submit'));
        $submit->setDecorators(array(
                   array('ViewHelper', array('helper' => 'formSubmit'))
                 ));

        $cancel = $form->createElement('button', 'cancel', array('label' => 'Cancel'));
        $cancel->setAttrib('id', 'cancel');
        $cancel->setDecorators(array(
                   array('ViewHelper', array('helper' => 'formButton'))
                ));
        
        $form->addElements(array($title, $url));

        $form->setElementDecorators(array(
                  'ViewHelper',
                  'Errors',
                  array('HtmlTag', array('tag' => 'div', 'class' => 'elm')),
                  array('Label', array('tag' => 'span')),
              ))
             ->addElements(array($submit, $cancel));

        if (isset($values['slideId'])) {

            $slideId = $form->createElement('hidden', 'slideId');
            $slideId->setValue($values['slideId']);
            $slideId->setDecorators(array(
                array('ViewHelper', array('helper' => 'formHidden'))
            ));

            $form->addElement($slideId);
        }
        return $form;
    }
}